package org.example.testdemo.interceptor;

import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.example.testdemo.tool.ApiResponse;
import org.springframework.web.servlet.HandlerInterceptor;

import java.io.IOException;

public class PageBlocking implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        try {
            //拿出session
            HttpSession session=request.getSession();
            //判断是否登录成功
            Object userId = session.getAttribute("userId");
            if(userId==null){
                response.sendRedirect("/page/logonPage");
                return false;
            }
            return true;
        }catch (Exception e){
            response.sendRedirect("/page/logonPage");
            return false;
        }
    }

}
